package com.ciaj.base.comm.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import com.ciaj.base.bean.PasswordEntity;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.UUID;

/**
 * @Author: ciaj.
 * @Date: 2020/8/14 13:06
 * @Description:
 * @version: 1.0
 */
public class PdUtil {
    public static String ALGORITHM_NAME_MD5 = "md5";
    public static int HASH_ITERATIONS = 1024;

    public static PasswordEntity getPassword(String password, String salt) {
        return new PasswordEntity(new SimpleHash(ALGORITHM_NAME_MD5, password, ByteSource.Util.bytes(salt), HASH_ITERATIONS).toHex(), new String(salt));
    }

    public static PasswordEntity getPassword(String password) {

        try {
            byte[] salt = SecureUtil.md5(UUID.randomUUID().toString().replace("-", "")).getBytes("utf-8");
            return new PasswordEntity(new SimpleHash(ALGORITHM_NAME_MD5, password, ByteSource.Util.bytes(salt), HASH_ITERATIONS).toHex(), new String(salt, "utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PasswordEntity();
    }

    public static void main(String[] args) {
        PasswordEntity password = getPassword("123456"); //6ac26cdb7217ee9636570ff7b22db6fe 33d4b7eb3f3f3ec58c61d4af0504a05f
//        PasswordEntity password = getPassword("123456", "33d4b7eb3f3f3ec58c61d4af0504a05f"); //24205511f720bbb5f675b5e12deb5df9
        System.out.println("password：" + password.getPassword() + " salt：" + password.getSalt());
    }
}
